package cn.sccdlg.spring5.tx.service.impl;

import cn.sccdlg.spring5.tx.dao.IAccountDao;
import cn.sccdlg.spring5.tx.domain.Account;
import cn.sccdlg.spring5.tx.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author RaoShuang
 * @emil 991207823@qq.com
 * @date 2020/10/21 23:41
 * 账户的业务层实现类
 * 事务控制应该都是在业务层
 */
@Service("accountService2")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class AccountServiceImpl2 implements IAccountService {
    @Autowired
    private IAccountDao accountDao;

    @Override
    public Account findAccountById(Integer accountId) {
        return accountDao.findAccountById(accountId);

    }

    //需要的是读写型事务配置
    @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
    @Override
    public void transfer(String sourceName, String targetName, Float money) {
        System.out.println("transfer....");
        //2.1根据名称查询转出账户
        Account source = accountDao.findAccountByName(sourceName);
        //2.2根据名称查询转入账户
        Account target = accountDao.findAccountByName(targetName);
        //2.3转出账户减钱
        source.setMoney(source.getMoney() - money);
        //2.4转入账户加钱
        target.setMoney(target.getMoney() + money);
        //2.5更新转出账户
        accountDao.updateAccount(source);

        int i = 1 / 0;

        //2.6更新转入账户
        accountDao.updateAccount(target);
    }
}
